package com.ebei.admin.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ebei.admin.common.constant.Constants;
import com.ebei.admin.common.util.AuthRealm;
import com.ebei.admin.model.entity.SysDict;
import com.ebei.admin.model.entity.SysDictDetail;
import com.ebei.admin.service.SysDictDetailService;
import com.ebei.admin.service.SysDictService;
import com.ebei.common.constant.CommonConstant;
import com.ebei.common.util.Query;
import com.ebei.common.util.Result;
import com.ebei.common.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 字典管理
 *
 * @author crazypenguin
 * @version 1.0
 * @created 2018/4/17
 */
@RestController
@RequestMapping("/dict")
@Api(value = "字典", description = "字典接口")
public class DictController extends BaseController {
    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private SysDictDetailService sysDictDetailService;
    /**
     * 通过ID查询字典信息
     *
     * @param id ID
     * @return 字典信息
     */
    @GetMapping("/{id}")
    public SysDict dict(@PathVariable Integer id) {
        return sysDictService.selectById(id);
    }

    /**
     * 分页查询字典信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @ApiOperation(value = "查询字典项", notes = "分页查询字典列表")
    @ApiImplicitParam(name = "params", value = "分页对象", required = true)
    @GetMapping("/dictPage")
    public Result<Page> dictPage(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        if(params.containsKey("_t")){
            params.remove("_t");
        }

        EntityWrapper ew = new EntityWrapper<>();

        if(params.get("dictName") != null && !"".equals(params.get("dictName"))){
            ew.like("dict_name",params.get("dictName").toString());
            params.remove("dictName");
        }
        if(params.get("dictCode") != null && !"".equals(params.get("dictCode"))){
            ew.like("dict_code",params.get("dictCode").toString());
            params.remove("dictCode");
        }
        if(AuthRealm.getPrincipal().getOrgId() != null){
            ew.eq("company_id",AuthRealm.getPrincipal().getOrgId());
        }

        Result<Page> result = new Result<>();
        result.setErrorCode(Result.SUCCESS);
        result.setErrorMessage("获取成功");
        result.setData(sysDictService.selectPage(new Query<>(params), ew));

        return result;
    }

    /**
     * 新增
     * @param sysDict
     * @return
     */
    @ApiOperation(value = "新增字典项", notes = "新增字典项")
    @ApiImplicitParam(name = "sysDict", value = "字典实体对象", required = true)
    @PostMapping("/dictAdd")
    public Result<Boolean> dictAdd(@RequestBody SysDict sysDict){
        Result<Boolean> result = new Result<>();
        sysDict.setCreateTime(new Date());
        sysDict.setUpdateTime(new Date());
        sysDict.setCompanyId(AuthRealm.getPrincipal().getOrgId());
        if(sysDictService.insert(sysDict)){
            result.setErrorCode(Result.SUCCESS);
            result.setErrorMessage("新增字典项成功");
        }else{
            result.setErrorCode(Result.FAIL);
            result.setErrorMessage("新增字典项失败");
        }
        return result;
    }

    @ApiOperation(value = "修改字典项", notes = "修改字典项")
    @ApiImplicitParam(name = "sysDict", value = "字典实体对象", required = true)
    @PostMapping("/dictUpd")
    public Result<Boolean> dictUpd(@RequestBody SysDict sysDict){
        Result<Boolean> result = new Result<>();
        sysDict.setUpdateTime(new Date());
        if(sysDictService.updateById(sysDict)){
            result.setErrorCode(Result.SUCCESS);
            result.setErrorMessage("修改字典项成功");
        }else{
            result.setErrorCode(Result.FAIL);
            result.setErrorMessage("修改字典项失败");
        }
        return result;
    }

    @ApiOperation(value = "删除字典项", notes = "删除字典项")
    @ApiImplicitParam(name = "dictId", value = "字典id", required = true)
    @PostMapping("/dictDel")
    public Result<Boolean> dictDel(@RequestBody Map<String, Object> params){
        Result<Boolean> result = new Result<>();
        Integer dictId = Integer.valueOf(params.get("dictId").toString());
        if(dictId == null){
            result.setErrorMessage("主键为空");
            result.setErrorCode(Result.FAIL);
            return result;
        }
        return sysDictService.dictDel(dictId);
    }

    @ApiOperation(value = "获取字典详情列表", notes = "根据字典项获取详情列表")
    @ApiImplicitParam(name = "dictId", value = "字典id", required = true)
    @GetMapping("/getDictDetailList")
    public List<SysDictDetail> getDictDetailList(@RequestParam Integer dictId){
        return sysDictDetailService.findDictDetailsByDictId(dictId);
    }

    @ApiOperation(value = "根据字典编码获取详情")
    @ApiImplicitParam(name = "dictCode", value = "字典code", required = true)
    @GetMapping("/getDictDetailListByDictCode")
    public List<SysDictDetail> getDictDetailList(@RequestParam String dictCode){
        return sysDictDetailService.findDictDetailsByDictCode(dictCode);
    }

    @ApiOperation(value = "新增字典详情", notes = "新增字典详情")
    @ApiImplicitParam(name = "sysDictDetail", value = "字典详情实体对象", required = true)
    @PostMapping("/dictDetailAdd")
    public Result<Boolean> dictDetailAdd(@RequestBody SysDictDetail sysDictDetail){
        Result<Boolean> result = new Result<>();
        sysDictDetail.setCreateTime(new Date());
        sysDictDetail.setUpdateTime(new Date());
        if(sysDictDetailService.insert(sysDictDetail)){
            result.setErrorMessage("新增详情成功");
            result.setErrorCode(Result.SUCCESS);
        }else{
            result.setErrorMessage("新增详情失败");
            result.setErrorCode(Result.FAIL);
        }
        return result;
    }

    @ApiOperation(value = "修改字典详情", notes = "修改字典详情")
    @ApiImplicitParam(name = "sysDictDetail", value = "字典详情实体对象", required = true)
    @PostMapping("/dictDetailUpd")
    public Result<Boolean> dictDetailUpd(@RequestBody SysDictDetail sysDictDetail){
        Result<Boolean> result = new Result<>();
        sysDictDetail.setUpdateTime(new Date());
        if(sysDictDetailService.updateById(sysDictDetail)){
            result.setErrorMessage("修改详情成功");
            result.setErrorCode(Result.SUCCESS);
        }else{
            result.setErrorMessage("修改详情失败");
            result.setErrorCode(Result.FAIL);
        }
        return result;
    }

    @ApiOperation(value = "删除字典详情", notes = "删除字典详情")
    @ApiImplicitParam(name = "detailId", value = "字典详情id", required = true)
    @PostMapping("/dictDetailDel")
    public Result<Boolean> dictDetailDel(@RequestBody Map<String, Object> params){
        Result<Boolean> result = new Result<>();
        Integer detailId = Integer.valueOf(params.get("detailId").toString());
        SysDictDetail sysDictDetail = sysDictDetailService.selectById(detailId);
        sysDictDetail.setDelFlag(CommonConstant.STATUS_DEL);
        if(sysDictDetailService.updateById(sysDictDetail)){
            result.setErrorMessage("删除详情成功");
            result.setErrorCode(Result.SUCCESS);
        }else{
            result.setErrorMessage("删除详情失败");
            result.setErrorCode(Result.FAIL);
        }
        return result;
    }

    @ApiOperation(value = "获取项目属性")
    @GetMapping("/getProjectAttr")
    public List<SysDictDetail> getProjectProperty() {
        return sysDictDetailService.findDictDetailsByDictCode(Constants.PROJECT_ATTRIBUTE);
    }

    @ApiOperation(value = "获取行业领域")
    @GetMapping("/getIndustrySector")
    public List<SysDictDetail> getIndustrySector() {
        return sysDictDetailService.findDictDetailsByDictCodeAndCompanyId(Constants.INDUSTRY_SECTOR,AuthRealm.getPrincipal().getOrgId());
    }

    @ApiOperation(value = "获取风险类型")
    @GetMapping("/getRiskLevel")
    public List<SysDictDetail> getRiskLevel() {
        return sysDictDetailService.findDictDetailsByDictCode(Constants.RISK_LEVEL);
    }

}
